<?php namespace App\Models\Cxhshop;

use App\Models\Base\CxhshopBase;
use Illuminate\Support\Facades\DB;
// use Illuminate\Support\Facades\Model;
use App\Models\Cxhshop\Order;
class Address extends CxhshopBase {  

  
    public $timestamps = false;//这里表示的是不添加时间
    protected $table = 'ims_ewei_shop_member_address';
    public static $rules = [
        'realname'=>'required',
        'city'=>'required',
        'mobile'=>'required',
        'province'=>'required',
        'area'=>'required',
        'address'=>'required',
        'city'=>'required',
        'isdefault'=>'required'

    ];
    //订单收货地址列表
    public function getaddress($uid,$page,$number){    
        $field = ['b.id','b.realname','b.mobile','b.province','b.city','b.area','b.address','b.isdefault','b.deleted'];
        $query = self::query();
        $date =DB::connection('jmft_cxh')->table('ims_ewei_shop_member_address as b')
        ->join('ims_ewei_shop_member as a', 'a.openid', '=', 'b.openid')->where('a.member_uuid','=',$uid)->forPage($page,$number)->get($field);
        if(!empty($date)){
            $date = json_decode($date,true);
            return $date;
        }else{
            return false;
        }
    } 
    //收货删除
    protected function deladdress($id){ 
        $res = self::where('id',$id)->delete();
        return $res;
    }
    //更改默认收货地址
    public function defaultaddress($param){ 
        $query = self::query();
        $res[] =  self::where('openid',$param['openid'])->update(['isdefault'=>0]);
        $res = self::where('id',$param['id'])->update(['isdefault'=>1]);
        if($res){
            return true;
        }else{
            return false;
        }
       
    }
    //编辑收货地址
    protected function doaddress($condition){
        $id = $condition['id'];
        $param['openid'] =  $condition['openid'];
        $param['realname'] =  $condition['realname'];
        $param['mobile'] =  $condition['mobile'];
        $param['province'] =  $condition['province'];
        $param['city'] =  $condition['city'];
        $param['area'] =  $condition['area'];
        $param['address'] =  $condition['address'];
        $param['isdefault'] = $condition['isdefault'];
        if($param['isdefault']==1){
            $this->updatestatus($param['openid']);
        }
        $res = self::where('id',$id)->update($param);
        return $res;

    } 
    //添加收货地址
    protected function addaddress($condition){
        $param['openid'] =  $condition['openid'];
        $param['realname'] =  $condition['realname'];
        $param['mobile'] =  $condition['mobile'];
        $param['province'] =  $condition['province'];
        $param['city'] =  $condition['city'];
        $param['area'] =  $condition['area'];
        $param['address'] =  $condition['address'];
        $param['isdefault'] = $condition['isdefault'];
        $param['uniacid'] =1;
        if($param['isdefault']==1){
            $this->updatestatus($param['openid']);
        }
        $res = self::insert($param);
        return $res;
      
    } 
    //普遍更新默认地址
    public function updatestatus($openid){
        $res = self::where('openid',$openid)->update(['isdefault'=>'0']);
    }
   

}